#!/bin/sh
if [ $# -ge 2 ]
then
    printf '%s [revision]\n' "$0" >&2
    exit 1
fi
set -e -x
pwd="$PWD"
name=mbank-cli
revision="${1:-tip}"
date=$(hg log -r "$revision" --template='{date|isodate}' | head -c 10 | tr -d -)
version=$(hg cat -r "$revision" doc/NEWS | head -n1 | cut -d ' ' -f2 | tr -d '()')
released=$(hg cat doc/NEWS | head -n1 | grep -v -w UNRELEASED || true)
[ -n "$released" ] || version="${version}rc${date}"
sourceroot=$(mktemp -d -t "$name-source-XXXXXX")
export TAR_OPTIONS="--owner root --group root --mode a+rX --format ustar"
export GZIP='-9 -n'
mkdir -p "$sourceroot/$name-$version"
hg archive -r "$revision" "$sourceroot/$name-$version"
cd "$sourceroot"
make -C $name-*/doc
make -C $name-*/t/certs/
rm -rf $name-*/private/
rm -rf */.hg*
find $name-*/ -empty -delete
tar -czf "$pwd/$name-$version.tar.gz" */
rm -rf "$sourceroot"

# vim:ts=4 sts=4 sw=4 et
